//
//  RankViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/26.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "RankViewController.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import "RankDetailModel.h"
#import "SubjectCell.h"
#import "RankCell.h"
#import "CartoonRecommendDetailViewController.h"

@interface RankViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation RankViewController

- (void)dealloc
{
    [_rankArray release];
    [_name release];
    [_collectView release];
    [_myId release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.rankArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationItem.title = self.name;
    NSLog(@"ii = %@", self.name);
    [self startConnection];
    [self createCollectionView];
}

#pragma mark -
#pragma mark 网络解析
- (void)startConnection
{
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=List&ios=23&pad=0&a=rankInfo&id=%@", self.myId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dic in array) {
            RankDetailModel *model = [[RankDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.rankArray addObject:model];
        }
        [self.collectView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
//
//#pragma mark -
//#pragma mark 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 80, 10);
    flow.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 - 20, 170);
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    self.collectView.backgroundColor = [UIColor clearColor];
    [self.collectView registerClass:[RankCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    [self.view addSubview:self.collectView];
    [self.collectView release];
}

#pragma mark -
#pragma mark 实现cell协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.rankArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RankDetailModel *model = [self.rankArray objectAtIndex:indexPath.item];
    RankCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.showImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    cell.titleLabel.text = model.name;
    
    NSInteger red = arc4random() % 255;
    NSInteger green = arc4random() % 255;
    NSInteger blue = arc4random() % 255;
    UIColor *color = [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
    cell.rankLabel.backgroundColor = color;
    cell.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.item + 1];

    if (([model.is_over isEqualToString:[NSString stringWithFormat:@"%d", 1]])) {
        cell.curNumLabel.text = [NSString stringWithFormat:@"全%@集", model.cur_num];
    }
    if ([model.is_over isEqualToString:[NSString stringWithFormat:@"%d", 0]]) {
        cell.curNumLabel.text = [NSString stringWithFormat:@"至%@集", model.cur_num];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonRecommendDetailViewController *detail = [[CartoonRecommendDetailViewController alloc] init];
    RankDetailModel *model = [self.rankArray objectAtIndex:indexPath.item];
    detail.iD = model.video_id;
    detail.lastup = model.cur_num;
    detail.is_over = model.is_over;
    detail.titleName = model.name;
    [self.navigationController pushViewController:detail animated:YES];
}


@end
